from random import Random #引入随机数模块
from users.models import EmailVerifyRecord
from OnlineLearn.settings import EMAIL_FROM
from django.core.mail import send_mail #发送邮件模块

def generate_random_str(randomlength=8):
	radndom_str=''
	chars='AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789'
	length=len(chars) - 1
	random=Random()
	for i in range(randomlength):
		radndom_str+=chars[random.randint(0, length)]
	return radndom_str

def send_register_email(email, send_type="register"):
	#将邮箱验证码写入数据库
	email_record=EmailVerifyRecord()
	code=generate_random_str(16)
	email_record.code=code
	email_record.email=email
	email_record.send_type=send_type
	email_record.save()
	#发送邮件
	email_title=""
	email_body=""

	if send_type=="register":
		email_title="51分期注册激活链接"
		email_body="请点击下面的链接激活你的账号：http://127.0.0.1:8000/active/{0}".format(code)

		send_status=send_mail(email_title, email_body, EMAIL_FROM, [email])

		if send_status:
			pass
	elif send_type=="forget":
		email_title="51分期找回密码链接"
		email_body="请点击链接重置你的密码：http://127.0.0.1:8000/reset/{0}".format(code)

		send_status=send_mail(email_title, email_body, EMAIL_FROM, [email])